/*
 * @Date: 2025-01-19 11:51:00
 * @LastEditors: zl 1077167261@qq.com
 * @LastEditTime: 2025-02-14 10:14:45
 * @FilePath: \vue-admin\src\store\index.js
 */
import Vue from "vue";
import Vuex from "vuex";
import user from "./modules/user";
import menu from "./modules/menu";
import createPersistedState from "vuex-persistedstate";
import Cookies from "js-cookie";
Vue.use(Vuex);
// 配置 storage将 token存储到 cookie 中
export default new Vuex.Store({
  plugins: [
    createPersistedState({
      key: "hm--vuex-token", // 存储在 localStorage 中的键名，默认为 'vuex'
      // storage: window.localStorage, // 或者 window.localStorage，默认为 localStorage
      storage: {
        getItem: (key) => {
          return Cookies.get(key);
        },
        // Please see https://github.com/js-cookie/js-cookie#json, on how to handle JSON.
        setItem: (key, value) =>
          Cookies.set(key, value, {
            expires: new Date(Date.now() + 1000 * 60 * 60 * 2),
          }),
        removeItem: (key) => Cookies.remove(key),
      },
      paths: ["user.token"], // 指定要持久化的 state 的路径，默认为所有 state
    }),
  ],
  modules: {
    user,
    menu,
  },
});
